<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Reply;
use Wjxcodes\Express\Express;
use JPush\Client as JPush;
use App\Events\TestEvent;
use Illuminate\Support\Facades\Storage;

class TestController extends Controller
{
    //
	public function test(User $user,Express $express)
	{
		// $reply = new Reply();

		// $reply->topic_id = 1;
		// $reply->user_id = 2;
		// $reply->content = '测试消息通知';
		// $reply->save();
		// event(new TestEvent());
        // ding()->at(['15639303436'])->text(config('app.name') . "\n" . "程序出错提醒");

        
	}

	public function uploadImg(Request $request)
	{
		$image = $request->image;
		$is_base = $request->is_base;
		if($is_base == 1){
			$fileContents = base64_decode($image);	
			$extension = 'png';		
			$disk = Storage::disk('qiniu');
	        $folder_name = "uploads/images/avatar/" . date("Ym/d", time());
	        
	        $filename = '1_' . time() . '_' . str_random(10) . '.' . $extension;

	       	$filepath = $folder_name.$filename;

	        $res = $disk->put($filepath, $fileContents);
	        if($res){
	        	return [
		            // 'path' => config('app.url') . "/$folder_name/$filename",
		            'path' => env('QINIU_DOMAIN').'/'.$filepath,
		        ];
	        }else{
	        	return false;
	        }		
		}else{
			$fileContents = $request->File('image');
        	$extension = strtolower($fileContents->getClientOriginalExtension()) ?: 'png';
        	$disk = Storage::disk('qiniu');
	        $folder_name = "uploads/images/avatar/" . date("Ym/d", time());
	        
	        $filename = '1_' . time() . '_' . str_random(10) . '.' . $extension;
	       
	        $filepath = $disk->put($folder_name, $fileContents);
	        if($filepath){
	        	return [
		            // 'path' => config('app.url') . "/$folder_name/$filename",
		            'path' => env('QINIU_DOMAIN').'/'.$filepath,
		        ];
	        }else{
	        	return false;
	        }
		}

		
	}
	
	public function get_notify(User $user)
	{
		$user = User::find(1);
		$user->notifications->markAsRead();

		// foreach ($user->notifications as $notification) {
		// 	dd($notification->data);
		// }

	}
}
